//
//  FamilyViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/20.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON

///家庭成员cell类型
enum FamilyCellType {
    case family
    case report
}

/// 绑卡状态
enum CardStatus: Int {
    case unbound
    case bound
    case reviewing
}

/// 绑卡类型
/// unbound - 0 未绑卡
/// selfpaid - 1 自费卡
/// medicare - 2 医保卡
/// double - 3 双卡
enum CardType: Int {
    case unbound
    case selfpaid
    case medicare
    case double
    
    var description: String {
        switch self {
        case .unbound: return "未绑定"
        case .selfpaid: return "自费卡"
        case .medicare: return "医保卡"
        case .double: return "自费卡 医保卡"
        }
    }
    
    var textColor: UIColor? {
        switch self {
        case .unbound: return UIColor(hex: "#F54C4C")
        default: return .mainColor
        }
    }
}

struct FamilyModel: HandyJSON {
    var patientId: String?
    var phone: String?
    var name: String?
    /// 关系id 查询详情接口使用
    var id: String?
    var relation: String?
    
    /// 是否绑定卡片
    var isBindingCard: Int = -1
    var cardType: Int = -1
    
    /// 收货人姓名
    var consigneeName: String?
    /// 收货人手机号
    var consigneePhone: String?
    /// 收货地址
    var addr: String?
}

struct FamilyRowViewModel {
    let family: FamilyModel
    
    var id: String { return family.id ?? "" }
    var patientId: String { return family.patientId ?? "" }
    var phone: String { return family.phone ?? "" }
    var name: String { return family.name ?? "" }
    var relationId: String { return family.id ?? "" }
    var relation: String { return family.relation ?? "" }
    var cardType: CardType { return CardType(rawValue: family.cardType) ?? .unbound }
    var cardStatus: CardStatus { return CardStatus(rawValue: family.isBindingCard) ?? .unbound }
    
    var receiveName: String { return family.consigneeName ?? "" }
    var receivePhone: String { return family.consigneePhone ?? "" }
    var receiveAddress: String { return family.addr ?? "" }
    var receiveId: String { return family.id ?? "" }
    
    var cardText: String {
        switch cardStatus {
        case .unbound: return cardType.description
        case .bound: return cardType.description
        case .reviewing: return "审核中"
        }
    }
}

struct FamilyViewModel {
    let models: [FamilyModel]
    
    var numberOfRows: Int { return models.count }
    
    func viewModel(for indexPath: IndexPath) -> FamilyRowViewModel {
        return FamilyRowViewModel(family: models[indexPath.row])
    }
    
    static func getFamilyList(success: @escaping (_ models: [FamilyModel]) -> Void,
                              failure: @escaping (_ errorMsg: String?) -> Void)
    {
        let paras = ["userId": UserDefaults.userId]
        HttpTool.shared.request(target: ApiManager.familyList(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let models = JSONDeserializer<FamilyModel>.deserializeModelArrayFrom(json: json["data"].description) as? [FamilyModel] {
                    success(models)
                } else {
                    failure("请求数据发生错误，稍后重试")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
    
    /// 获取收货地址列表
    static func getReceiveAddressList(success: @escaping (_ models: [FamilyModel]) -> Void, failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["userId": UserDefaults.userId]
        HttpTool.shared.request(target: ApiManager.receiveAddressList(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let models = JSONDeserializer<FamilyModel>.deserializeModelArrayFrom(json: json["data"].description) as? [FamilyModel] {
                    success(models)
                } else {
                    failure("请求数据发生错误，稍后重试")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
}
